﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using System.Text.RegularExpressions;
using DevExpress.XtraGrid.Views.Grid;

namespace LhFormsControlLibrary
{
    public partial class GridPage : UserControl
    {
        public GridPage()
        {
            InitializeComponent();
            Ini();
        }

        private string PageSql, TotalSql;
        private int RowCount;
        private int Total;
        private int Index;
        public delegate void GridViewDoubleClickEvent(object sender, EventArgs e);
        public event GridViewDoubleClickEvent GridViewDoubleClickOut;
        public delegate void CustomUnboundColumnDataEvent(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e);
        /// <summary>
        /// 单元格显示图片事件
        /// </summary>
        public event CustomUnboundColumnDataEvent CustomUnboundColumnDataOut;

        public delegate void NavPageChangeEvent(object sender, int PageNo, int Total);
        public event NavPageChangeEvent PageChangeEvent;
        public delegate void RowCellClickEvent(object sender, RowCellClickEventArgs e);
        /// <summary>
        /// 点击单元格事件
        /// </summary>
        public event RowCellClickEvent RowCellClickEventOut;
        public delegate void RowCellStyleEvent(object sender, RowCellStyleEventArgs e);
        /// <summary>
        /// 根据指定条件，改变行背景色
        /// </summary>
        public event RowCellStyleEvent RowCellStyleEventOut;
        private delegate void GridDataSource(DataTable dataTable);
        private delegate void GridDataRow(int RowCount);

        #region 属性
        [Description("每页条数")]
        [DefaultValue(50)]
        public int PageSize
        {
            get; set;
        }
        [Description("当前页数字段，用于输出字段内容")]
        [DefaultValue("PageNo")]
        public string PageNoName
        {
            get; set;
        }
        [Description("每页数量字段，用于输出字段内容")]
        [DefaultValue("PageSize")]
        public string PageSizeName
        {
            get; set;
        }
        #endregion

        #region 公有方法
        public int GetPageNo()
        {
            return this.Index;
        }
        public string GetPageSql()
        {
            return this.PageSql;
        }
        public string GetTotalSql()
        {
            return this.TotalSql;
        }
        public GridView GetGridView()
        {
            return this.gridView1;
        }
        public void SetDataSource(DataTable dataTable, string PageSql, string TotalSql)
        {
            ProSetDataSource(dataTable, PageSql, TotalSql);
        }
        public void SetDataSource(DataTable dataTable)
        {
            if (this.gridControl1.InvokeRequired)
            {
                this.gridControl1.Invoke(new GridDataSource(SetDataSource), new object[] { dataTable });
            }
            else
            {
                ProSetDataSource(dataTable, null, null);
            }
        }

        public void SetRowCount(int RowCount)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new GridDataRow(SetRowCount), new object[] { RowCount });
            }
            else
            {
                this.RowCount = RowCount;
                this.Total = RowCount / PageSize;
                if (RowCount % PageSize > 0)
                {
                    this.Total++;
                }
                ChangeButton(null);
            }
        }
        public int GetTotol()
        {
            return this.Total;
        }
        #endregion

        #region 私有方法
        private void ProSetDataSource(DataTable dataTable, string PageSql, string TotalSql)
        {
            this.gridControl1.DataSource = dataTable;
            this.PageSql = PageSql;
            this.TotalSql = TotalSql;
        }
        private void Ini()
        {
            PageSize = 50;
            RowCount = 0;
            Total = 0;
            Index = 1;
            PageNoName = "PageNo";
            PageSizeName = "PageSize";
            SetTotolCount();
            IniButton();
        }
        private void SetTotolCount()
        {
            this.labelControl1.Text = string.Format("总条数：{3}    每页条数：{0}      当前页数：{1}    总页数：{2}"
                , PageSize
                , Index
                , Total
                , RowCount
                );
        }
        private void SetLast(EventArgs e)
        {
            this.Index = Total;
            ChangeButton(e);
        }
        private void SetFirst(EventArgs e)
        {
            this.Index = 1;
            ChangeButton(e);
        }
        private void SetNext(EventArgs e)
        {
            if (this.Index < this.Total + 1)
                this.Index++;
            ChangeButton(e);
        }
        private void SetPrev(EventArgs e)
        {
            if (this.Index > 0)
                this.Index--;
            ChangeButton(e);
        }
        private void SetGo(EventArgs e)
        {
            string GoNumber = this.textEdit1.Text.Trim();
            if (GoNumber != null && GoNumber.Length > 0)
            {
                Regex rx = new Regex("^[0-9]*$");
                if (rx.IsMatch(GoNumber))
                {
                    int myNo = int.Parse(GoNumber);
                    if (myNo > 0 && myNo < Total + 1)
                    {
                        this.Index = myNo;
                    }
                    if (myNo < 1)
                    {
                        this.Index = 1;
                        this.textEdit1.Text = this.Index.ToString();
                    }
                    if (myNo > Total)
                    {
                        this.Index = Total;
                        this.textEdit1.Text = this.Index.ToString();
                    }
                    ChangeButton(e);
                }
            }
        }
        private void IniButton()
        {
            this.simpleButton3.Enabled = false;
            this.textEdit1.Enabled = false;
            this.simpleButton1.Enabled = false;
            this.simpleButton2.Enabled = false;
            this.simpleButton4.Enabled = false;
            this.simpleButton5.Enabled = false;
        }
        private void ChangeButton(EventArgs e)
        {
            if (Total > 1)
            {
                this.simpleButton3.Enabled = Total > 0 ? true : false;
                this.textEdit1.Enabled = this.simpleButton3.Enabled;
                this.simpleButton1.Enabled = this.Index == this.Total ? false : true;
                this.simpleButton2.Enabled = this.Index < this.Total ? true : false;
                this.simpleButton4.Enabled = this.Index == 1 ? false : true;
                this.simpleButton5.Enabled = this.Index > 1 ? true : false;
            }
            else
            {
                IniButton();
            }
            SetTotolCount();
            if (e != null)
            {
                if (PageChangeEvent != null)
                    PageChangeEvent(this, this.Index, this.Total);
            }
        }
        #endregion

        #region 事件
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            SetLast(e);
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            SetNext(e);
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            SetGo(e);
        }

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            SetPrev(e);
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            SetFirst(e);
        }

        private void gridView1_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            RowCellClickEventOut?.Invoke(sender, e);
        }

        private void gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            RowCellStyleEventOut?.Invoke(sender, e);
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            GridViewDoubleClickOut?.Invoke(sender, e);
        }

        private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            CustomUnboundColumnDataOut?.Invoke(sender, e);
        }

        private void textEdit1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (this.simpleButton3.Enabled)
                    SetGo(e);
            }
        }
        #endregion
    }
}
